<?php

namespace Admin\Models\ask;

use Phalcon\Mvc\Model;
class XhaskAnswer extends Model
{

    /**
     *
     * @var integer
     */
    public $iAAId;

    /**
     *
     * @var integer
     */
    public $iAQId;

    /**
     *
     * @var integer
     */
    public $iASId;

    /**
     *
     * @var integer
     */
    public $iAOId;

    /**
     *
     * @var string
     */
    public $mAnswer;

    /**
     *
     * @var integer
     */
    public $iUserId;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        $this->setSource("XHAsk_Answer");
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'XHAsk_Answer';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhaskAnswer[]
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhaskAnswer
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    /**
     * 保存答案
     *
     * @param $data
     * @return bool
     *
     * create by ck 20171214
     */
    public static function createRecord($data){
        return (new self($data))->save();
    }

    /**
     * 根据问卷ID获取答题人数
     *
     * @param $surveyId
     * @return mixed
     *
     * create by ck 20160926
     */
    public static function getAnswerNumBySurveyId($surveyId)
    {
        return self::find(array(
            'columns'       => 'iAQId,count(DISTINCT(iUserId)) as iAnswerNum',
            'conditions'    => 'iASId = :iASId:',
            'group'         => 'iAQId',
            'bind'          => array(
                'iASId'         => $surveyId
            )
        ))->toArray();
    }

    /**
     * 根据问卷ID获取题目答案详情
     *
     * @param $surveyId
     * @return mixed
     *
     * create by ck 20160926
     */
    public static function getAnswerDetailBySurveyId($surveyId)
    {
        return self::find(array(
            'columns'       => 'iAAId,iAQId,iAOId,mAnswer,count(DISTINCT(iUserId)) AS iAnswerNum,iUserId',
            'conditions'    => 'iASId = :iASId:',
            'group'         => 'iAQId,iAOId,mAnswer',
            'bind'          => array(
                'iASId'         => $surveyId
            )
        ))->toArray();
    }

//    public static function checkRepeat($teacherId,$surveyId)
//    {
//        $checkRepeat = XhaskAnswer::findFirst(array(
//            'conditions'    => 'iUserId=:iUserId: AND iASId=:iASId:',
//            'bind'          => array(
//                'iUserId'   => $teacherId,
//                'iASId'     => $surveyId,
//            )
//        ));
//        return $checkRepeat?true:false;
//    }
}
